package org.agilelang.generator.javastruts2;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;
import javax.imageio.ImageIO;

public class Util {

    protected static String readFileAsString(Reader reader) {
        StringBuffer fileData = new StringBuffer(100000);
        char[] buf = new char[1024];
        int numRead = 0;
        try {
            while ((numRead = reader.read(buf)) != -1) {
                String readData = String.valueOf(buf, 0, numRead);
                fileData.append(readData);
                buf = new char[1024];
            }
            reader.close();
        } catch (IOException ex) {
            //Logger.getLogger(GenerateHibernateModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return fileData.toString();
    }

    protected void createfile(String Name, String path, String Contenido) {
        Writer output = null;
        System.out.println(path + File.separator + Name);
        File file = new File(path + File.separator + Name);
        try {
            output = new BufferedWriter(new FileWriter(file));
            output.write(Contenido);
            output.close();
        } catch (IOException ex) {
            System.out.println(ex.toString());
        }
    }

    public void createDirectory(String path) {
        //verificamos si existe fichero, sino lo creamos
        File folder = new File(path);
        if (!folder.exists()) {
            folder.mkdirs();
            System.out.println("createDirectory : " + folder.getAbsolutePath());
        }

    }

    protected String readStream(String tpl) {
        Reader reader = null;
        try {
            InputStream is = this.getClass().getResourceAsStream("/fgl/tpl/"+tpl);
            if (is != null) {
                try {
                    reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String file = readFileAsString(reader);
        return file;
    }
    
    protected String readStreamOther(String tpl) {
        Reader reader = null;
        try {
            InputStream is = this.getClass().getResourceAsStream("/fgl/tpl/other/"+tpl);
            if (is != null) {
                try {
                    reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String file = readFileAsString(reader);
        return file;
    }
    
        protected String readStreamController(String tpl) {
        Reader reader = null;
        try {
            InputStream is = this.getClass().getResourceAsStream("/fgl/tpl/controller/"+tpl);
            if (is != null) {
                try {
                    reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String file = readFileAsString(reader);
        return file;
    }
    
    protected String readStreamMenu(String tpl) {
        Reader reader = null;
        try {
            InputStream is = this.getClass().getResourceAsStream("/fgl/tpl/menu/"+tpl);
            if (is != null) {
                try {
                    reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String file = readFileAsString(reader);
        return file;
    }
    
    protected String readStreamDB(String tpl) {
        Reader reader = null;
        try {
            InputStream is = this.getClass().getResourceAsStream("/fgl/tpl/db/"+tpl);
            if (is != null) {
                try {
                    reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String file = readFileAsString(reader);
        return file;
    }
    
    protected String readStreamControllerCRUD(String tpl) {
        Reader reader = null;
        try {
            InputStream is = this.getClass().getResourceAsStream("/fgl/tpl/controller_crud/"+tpl);
            if (is != null) {
                try {
                    reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String file = readFileAsString(reader);
        return file;
    }
    
    protected String readStreamConfig(String tpl) {
        Reader reader = null;
        try {
            InputStream is = this.getClass().getResourceAsStream("/fgl/tpl/config/"+tpl);
            if (is != null) {
                try {
                    reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String file = readFileAsString(reader);
        return file;
    }

    public String readfile(String file) {
        String strfile="";
        try {
            FileInputStream fstream = new FileInputStream(file);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            while ((strLine = br.readLine()) != null) {
                strfile = strfile + strLine;
            }
            in.close();
        } catch (Exception e) {//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
        return strfile;
    }
    
    public void createImageIcon(String pathint, String path) {        
        BufferedImage image = null;
        try { 
            java.net.URL url = getClass().getResource("/fgl/tpl/" + pathint);
            image = ImageIO.read(url);
            ImageIO.write(image, "png",new File(path)); 
        } catch (IOException e) {
        	e.printStackTrace();        }
        System.out.println("Done");
    }
    
    public void fileRelocate(String filename, String path) {
        String contenido = this.readStream(filename);
        Writer output = null;
        System.out.println(path);
        File file = new File(path);
        try {
            output = new BufferedWriter(new FileWriter(file));
            output.write(contenido);
            output.close();
        } catch (IOException ex) {
            System.out.println(ex.toString());
        }        
    }
           
            
    
   
}
